package com.showsu.editor.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.showsu.editor.entity.doo.User;
import com.showsu.editor.entity.doo.VipGoods;
import com.showsu.editor.entity.dto.GiveVip2UserDTO;
import com.showsu.editor.entity.dto.PostXhsEditorTemplateTitleDTO;
import com.showsu.editor.entity.po.TblUser;
import com.showsu.editor.entity.po.TblXhsEditorTemplateTitle;
import com.showsu.editor.enums.ResultCode;
import com.showsu.editor.enums.UserVipEnum;
import com.showsu.editor.enums.VipLevelEnum;
import com.showsu.editor.service.IUserPrivilegeService;
import com.showsu.editor.service.IUserService;
import com.showsu.editor.service.IVipGoodsService;
import com.showsu.editor.service.IXhsEditorTemplateTitleService;
import com.showsu.editor.utils.BsResult;
import com.showsu.editor.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author: William
 * @Description: 内部管理用的后门，供内部运营人员调用，后续多的话可以单独搞个cms系统
 * @Date: Created in 17:34 2024/12/6
 * @Modified By:
 */
@RestController
@RequestMapping("/api/manage")
@Api(value = "ManageController", tags = "内部管理", description = "内部管理相关接口", position = 3)
@Slf4j
public class ManageController extends BaseController {

    private static String managetoken = "uMkZff7vHMqIQcZsHmvrMDB2dfptX3VK";

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserPrivilegeService userPrivilegeService;

    @Autowired
    private IVipGoodsService vipGoodsService;

    @Autowired
    private IXhsEditorTemplateTitleService editorTemplateTitleService;

    @ApiOperation("")
    @PostMapping("/giveVip2User")
    @ResponseBody
    public BsResult giveVip2User(@RequestBody GiveVip2UserDTO req) {
        String token = getRequest().getHeader("bh1wO2ea");
        if (!managetoken.equals(token)) {
            return BsResult.error(ResultCode.UN_AUTHORIZED);
        }
        if (req.getUserId() == null || req.getVipLevelCode() == null) {
            return BsResult.error(ResultCode.PARAM_MISS);
        }
        User user = userService.queryById(req.getUserId());
        if (user == null) {
            return BsResult.error(ResultCode.PARAM_VALID_ERROR);
        }
        VipLevelEnum vipLevel = VipLevelEnum.getByLevel(req.getVipLevelCode());
        if (vipLevel.getCode().equals(VipLevelEnum.FREE.getCode())) {
            return BsResult.error(ResultCode.PARAM_VALID_ERROR);
        }
        VipGoods vipGoods = vipGoodsService.queryByVipLevel(req.getVipLevelCode());
        if (vipGoods == null) {
            return BsResult.error(ResultCode.PARAM_VALID_ERROR);
        }
        //首先赋予用户会员身份
        TblUser updateUser = new TblUser();
        updateUser.setId(req.getUserId());
        updateUser.setVipUser(UserVipEnum.VIP.getCode());
        Date vipEndTime = DateUtil.addDay(new Date(), vipGoods.getDays());
        updateUser.setVipEndTime(vipEndTime);
        updateUser.setVipLevelCode(req.getVipLevelCode());
        userService.updateById(updateUser);
        //更新权限关系
        userPrivilegeService.handleUserPrivilege(req.getUserId(), vipLevel, vipEndTime, "manageInterface");
        return BsResult.success("成功为用户赋予套餐");
    }


    @ApiOperation("")
    @PostMapping("/editor/template/title")
    @ResponseBody
    public BsResult postEditorTemplateTitle(@RequestBody PostXhsEditorTemplateTitleDTO req) {
        String token = getRequest().getHeader("bh1wO2ea");
        if (!managetoken.equals(token)) {
            return BsResult.error(ResultCode.UN_AUTHORIZED);
        }

        if (StringUtils.isBlank(req.getTag()) || CollectionUtil.isEmpty(req.getTitle())) {
            return BsResult.error(ResultCode.PARAM_MISS);
        }
        req.getTitle().forEach(title->{
            TblXhsEditorTemplateTitle templateTitle=new TblXhsEditorTemplateTitle();
            templateTitle.setTitle(title);
            String[] tagArr=req.getTag().split("_");
            String tags="";
            for(String tag :tagArr){
                if(StringUtils.isBlank(tags)){
                    tags=("@"+tag+"@");
                }else{
                    tags+=("@"+tags.replaceAll("@","")+"_"+tag+"@");
                }


            }
            templateTitle.setTags(tags);

            if(req.getSort()!=null){
                templateTitle.setSort(req.getSort());
            }

            editorTemplateTitleService.save(templateTitle);
        });


        return BsResult.success("");
    }
}
